package cn.legend.travel.tour.team.controller;

import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.web.JsonResult;
import cn.legend.travel.tour.team.pojo.param.RequestParams;
import cn.legend.travel.tour.team.pojo.vo.TourSearchVO;
import cn.legend.travel.tour.team.schedule.TourSearchSchedule;
import cn.legend.travel.tour.team.service.ITourService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tour")
@Api(tags = "旅游团模块")
@Slf4j
public class TourController {

    @Autowired
    ITourService tourService;


    @GetMapping("/list/general/{page}")
    @ApiOperation("综合排序功能")
    public JsonResult listForGeneral(@PathVariable String page){

        return JsonResult.ok(tourService.listForGeneral(Integer.parseInt(page)));
    }
    @GetMapping("/list/saleCount/{page}")
    @ApiOperation("销量优先功能")
    public JsonResult listForSaleCount(@PathVariable String page){

        return JsonResult.ok(tourService.listForSaleCount(Integer.parseInt(page)));
    }

    @GetMapping("/list/priceUp/{page}")
    @ApiOperation("价格从低到高功能")
    public JsonResult listForPriceUp(@PathVariable String page){

        return JsonResult.ok(tourService.listForPriceASC(Integer.parseInt(page)));
    }

    @GetMapping("/list/priceDown/{page}")
    @ApiOperation("价格从高到低功能")
    public JsonResult listForPriceDown(@PathVariable String page){

        return JsonResult.ok(tourService.listForPriceDesc(Integer.parseInt(page)));
    }

    @GetMapping("/list/hot")
    @ApiOperation("热销功能")
    public JsonResult listForHotItem(){

        return JsonResult.ok(tourService.listForHotItem());
    }

    @PostMapping("/list/search")
    @ApiOperation("筛选和搜索功能")
    public JsonResult searchList(@RequestBody RequestParams params){
        String keyword = params.getKeyword();
        Integer page = params.getPage();
        String city = params.getCity();
        //当搜索关键字为空或不存在时，按各自的逻辑跳转搜索
        if(  keyword == null|| keyword.isEmpty()){
            return JsonResult.fail(ServiceCode.ERROR_CONFLICT,"请输入关键字后再进行查询");
        }

        if(page == null || page <= 0 ){
            page = 1;
        }
        try {
            PageData<TourSearchVO> search = tourService.search(keyword,city, page);
            if(search.getList().size() == 0 ){
                return JsonResult.fail(ServiceCode.ERROR_NOT_FOUND,"未查询到相关信息");
            }
            return JsonResult.ok(search);
        } catch (Exception e) {
            String message = "未查询到相关信息";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
    }
    @PostMapping("/list/type/search")
    @ApiOperation("根据旅游类型进行查询")
    public JsonResult searchByType(@RequestBody RequestParams params){
        String type = params.getType();
        Integer page = params.getPage();
        if(page == null || page <= 0 ){
            page = 1;
        }
        if(  "all".equals(type)){
            return JsonResult.ok(tourService.listForGeneral(page));
        }
        try {
            PageData<TourSearchVO> search = tourService.searchByType(type, page);
            if(search.getList().size() == 0 ){
                return JsonResult.fail(ServiceCode.ERROR_NOT_FOUND,"未查询到相关信息");
            }
            return JsonResult.ok(search);
        } catch (Exception e) {
            String message = "未查询到相关信息";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

    }

    @GetMapping("/detail/{guideId}")
    @ApiOperation("旅游团详情功能")
    public JsonResult getItemByGuideId(@PathVariable String guideId){

        return JsonResult.ok(tourService.getItemByGuideId(Long.parseLong(guideId)));
    }


}
